<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Spreaduser_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }
	
	function insert_member($sdk_member){

		$str = $this->db->insert_string('sdk_spread_user', $sdk_member); 
		$this->db->query($str);
		$mid = $this->db->insert_id();

		return  $mid > 0 ? $mid : -2;
	}
	
	function get_sp_site(){
		
		$q = $this->db->query("SELECT a.*,b.mid from sdk_site a left join sdk_relationship b on a.siteid=b.siteid where a.siteid <> 23456789");
		return $q->result();
		
	}
	
	function insert_tgwz($mid='',$siteid =''){

		return $this->db->insert('sdk_relationship',array('mid'=>$mid,'siteid'=>$siteid));
		
	}
	
	function spreadusernumrows(){
	
		$q = $this->db->query("select * from sdk_spread_user");
	
		return $q->num_rows();
	
	}
	
	function spreaduser_list($limit,$size){
		
		$sql = "select * from sdk_spread_user limit ".$limit*$size.",".$size;
		
		$q = $this->db->query($sql);
		
		return $q->result();
	}
	
	function is_exist_member($email){

		$sql = "select * from sdk_spread_user where email = ?";
		$q = $this->db->query($sql , $email);
		
		return $q->num_rows() > 0 ? true : false;
	}
	
	function member_login($sdk_member){
		$sql = "select * from sdk_spread_user where email = ?";

		$q = $this->db->query($sql, array($sdk_member->email)); 
		
		return $q->row();
	}
	
	function update_member_name($sdk_member){
		$data = array('firstname'=>$sdk_member->firstname, 'lastname'=>$sdk_member->lastname);
		$where = "mid='" . $sdk_member->uid . "'";
		
		$sql = $this->db->update_string('sdk_member', $data, $where);

		$this->db->query($sql); 
		
		return $this->db->affected_rows() > 0 ? 0 : -1; 
	}
	
	function update_member_password($sdk_member){
		$data = array('password'=>$sdk_member->newpassword);
		$where = "mid='$sdk_member->uid'" ;
		
		$sql = $this->db->update_string('sdk_spread_user', $data, $where);
		
		$this->db->query($sql); 
		
		return $this->db->affected_rows() > 0 ? 0 : -1;
	}
	
	function forget_password($sdk_member, $password){
		
		$data = array('password'=>$password);
		$where = "email='" . $sdk_member->email . "' and siteid='" . $sdk_member->siteid . "'";
		
		$sql = $this->db->update_string('sdk_member', $data, $where);
		
		$this->db->query($sql); 
		
		return $this->db->affected_rows() > 0 ? 0 : -1;
	}
	
	function get_relationship($mid = ''){
		
		$a = array();
		$sql = "select s.* from sdk_site as s join sdk_relationship as r where r.mid='$mid' and r.siteid=s.siteid";
		$query = $this->db->query($sql);
		if($query->num_rows()>0){
			$a['checkedsite'] = $query->result();
		}else{
			$a['checkedsite'] = '';
		}
		$sql = "select s.* from sdk_site as s join sdk_relationship as r where r.siteid=s.siteid";
		$query = $this->db->query($sql);
		$a['sites'] = $query->result();
		return $a;
		
	}
	
	function get_s_sites($mid = ''){
		
		$sql = "select a.sitename from sdk_site a join sdk_relationship b where a.siteid=b.siteid and b.mid=$mid";
		$q = $this->db->query($sql);
		return $q->result();
		
	}
	
	function insert_relationship($info){
		
		$q = $this->db->get_where('sdk_relationship',array('siteid'=>$info->siteid));
		if($q->num_rows()>0){
			return false;
		}
		$query = $this->db->insert('sdk_relationship',array('mid'=>$info->mid,'siteid'=>$info->siteid));
		if($this->db->affected_rows()>0){
			return true;
		}else{
			return false;
		}
	}
	
	function site_list(){
		
		$q = $this->db->get('sdk_site');
		
		$q = $q->result();

		foreach ($q as $k=>$v){
			if($v->siteid=="23456789"){
				unset($q[$k]);
				continue;
			}
		}
		
		return $q;
	}
	
	function member_list(){
		$sql = 'select * from sdk_member';
		
		$q = $this->db->query($sql);
		return $q->result();
	}

	function spread_detail($mid){
		$sql = "select * from sdk_spread_user where mid =" . $mid . ' limit 1';
		$q = $this->db->query($sql);	
		$sdk_spreaduser = $q->first_row();
	
		$sql = "select * from sdk_site ";
		$q = $this->db->query($sql);
		$sdk_site = $q->result();
		foreach ($sdk_site as $k=>$v){
			if($v->siteid=="23456789"){
				unset($sdk_site[$k]);
				continue;
			}
		}
	
		$a['spreaduser'] = $sdk_spreaduser;
		$a['sdk_site'] = $sdk_site;

		return $a;
	}
	
	function withdrawalnumrows(){
	
		$q = $this->db->query("select * from sdk_extract");
	
		return $q->num_rows();
	
	}
	
	function withdrawal_list($limit,$size,$mid = ''){
		
		if($mid){
			
			$sql = "select * from sdk_extract where mid='$mid' order by apptime";
		}else{
			
			$sql = "select * from sdk_extract order by apptime";
		}
		
		$sql .= " limit ".$limit*$size.",".$size;
		
		$q = $this->db->query($sql);
		return $q->result();
		
	}
	
	//显示order
	function sp_show_order($mid = '',$sdate = '',$edate = '',$tt = true,$limit = 0,$size = 15){

		$sql = "select a.*,c.sitename from sdk_order a join sdk_relationship b on b.mid=$mid and a.siteid=b.siteid ";
		
		if($sdate) $sql .= " and create_time > $sdate";
		
		if($edate) $sql .= " and create_time < $edate";
		
		$sql .= " join sdk_site c where a.siteid=c.siteid";
		
		if($tt) $sql .= " limit ".$limit*$size.",$size";
		
		$q = $this->db->query($sql);
		
		return $q->result();
		
	}
	
	//计算order
	function sp_order_total($mid = '',$sdate = '',$edate = ''){
		
		$sql = "select a.* from sdk_order a join sdk_relationship b where b.mid=$mid and a.siteid=b.siteid and a.status='Success'";
		
		if($sdate) $sql .= " and create_time > $sdate";
		
		if($edate) $sql .= " and create_time < $edate";
		
		$q = $this->db->query($sql);
		
		return $q->result();
		
		
		/* $a = array();
		$a['ack'] = 0;
		if($mid){		
			$q = $this->db->get_where('sdk_spread_order',array('mid'=>$mid));
			if($re = $q->result()){			
				foreach ($re as $k=>$v){				
					$q = $this->db->get_where('sdk_order',array('orderid'=>$v->orderid));
					$b = $q->row();
					$q = $this->db->get_where('sdk_site',array('siteid'=>$b->siteid));
					$q = $q->row();
					$b->sitename = $q->sitename;
					$a['order_list'][] = $b;
				}				
				return $a;
			}else{
				$a['ack'] = -1;//暂无订单
				return $a;
			}
		}else{
			$a['ack'] = -2;//查询失败
			return $a;
		} */
		
		
		
	}
	
	function insert_spread_order($sdk_order=''){
		
		$query = $this->db->get_where('sdk_spread_user',array('token'=>$sdk_order->token));
		
		if($query->num_rows>0){
			
			$q = $query->row();
			
			$this->db->insert('sdk_spread_order',array('mid'=>$q->mid,'orderid'=>$sdk_order->orderid,'siteid'=>$sdk_order->siteid,'create_time'=>time()));
			
			return $this->db->affected_rows() > 0 ? 0 : -1;
		}else{
			
			return -1;
		}
		
	}
	
	function site_percentage($mid='',$siteid = ''){
		
		if($mid&&$siteid){
			
			$q = $this->db->get_where('sdk_percentage',array('mid'=>$mid,'siteid'=>$siteid));
			
			if($q->num_rows()>0){
				
				$re = $q->row();
				
				return $re->percentage;
			}else{
				
				$query = $this->db->get_where('sdk_site',array('siteid'=>$siteid));
				
				$re = $query->row();
				
				return $re->percentage;
			}
		}
	}
	
	
	
}